package orderlayer

import (
	//"strconv"
	//"time"
	"encoding/json"
	"dhfshop/models/seller"
	"dhfshop/models/shopping"
	"github.com/astaxie/beego"
)

type AccountItem struct {
    Account      string   `json:"account"`        //账号
    AssignMoney  float32  `json:"assign_money"`   //分账金额
}

type AccountAll struct {
	Total        float32
	AccountItems []*AccountItem
}

func GetAssignMoney(orderItems []*shopping.OrderItem,partnerId,storeId,orderId int64)(*AccountAll, error) {
	type AssignAccount struct {
		Account      string      //账号
		AssignRatio  float32    //分账比例
		AssignMoney  float32    //分账金额
	}
	type AssignAll struct {
		ItemId         int64
		SkuId          int64
		Barcode        string     //商品备案编码
		AssignAccounts []*AssignAccount
	}
	//
	var assignArr []*AssignAll
	//beego.Debug("GetAssignMoney:", partnerId, storeId, orderId)
	fzStores, err := seller.GetFzStore(storeId)
	if err != nil {
	   return nil, err 
	}
	//beego.Debug("GetAssignMoney fzStores.FzGroupId:", fzStores.FzGroupId)
	_, err = seller.GetFzGroups(fzStores.FzGroupId)
	if err != nil {
	   return nil, err 
	}
	for _, value := range orderItems {
	   //beego.Debug("GetFzItem fzStores.FzGroupId:",fzStores.FzGroupId,value.Barcode)
	   if fzitems, err := seller.GetFzItem(fzStores.FzGroupId, value.Barcode); err == nil {
	       b, _ := json.Marshal(fzitems)
		   beego.Debug("fzitems:", string(b))
		   assignAll := new(AssignAll)
		   assignAll.ItemId = value.ItemId
		   assignAll.SkuId = value.SkuId
		   assignAll.Barcode = value.Barcode
		   if len(fzStores.Z1)<1&&len(fzStores.Z2)<1&&len(fzStores.Z3)<1&&len(fzStores.Z4)<1&&len(fzStores.Z5)<1 {
		       continue
		   }
		   if fzitems.R1 > 0 {
		       assignAccount := new(AssignAccount)
			   assignAccount.Account = fzStores.Z1
			   assignAccount.AssignRatio = fzitems.R1
			   assignAccount.AssignMoney = (value.Payment+value.ItemTax)*assignAccount.AssignRatio/100
			   assignAll.AssignAccounts = append(assignAll.AssignAccounts, assignAccount)
		   }
		   if fzitems.R2 > 0 {
		       assignAccount := new(AssignAccount)
			   assignAccount.Account = fzStores.Z2
			   assignAccount.AssignRatio = fzitems.R2
			   assignAccount.AssignMoney = (value.Payment+value.ItemTax)*assignAccount.AssignRatio/100
			   assignAll.AssignAccounts = append(assignAll.AssignAccounts, assignAccount)
		   }
		   if fzitems.R3 > 0 {
		       assignAccount := new(AssignAccount)
			   assignAccount.Account = fzStores.Z3
			   assignAccount.AssignRatio = fzitems.R3
			   assignAccount.AssignMoney = (value.Payment+value.ItemTax)*assignAccount.AssignRatio/100
			   assignAll.AssignAccounts = append(assignAll.AssignAccounts, assignAccount)
		   }
		   if fzitems.R4 > 0 {
		       assignAccount := new(AssignAccount)
			   assignAccount.Account = fzStores.Z4
			   assignAccount.AssignRatio = fzitems.R4
			   assignAccount.AssignMoney = (value.Payment+value.ItemTax)*assignAccount.AssignRatio/100
			   assignAll.AssignAccounts = append(assignAll.AssignAccounts, assignAccount)
		   }
		   if fzitems.R5 > 0 {
		       assignAccount := new(AssignAccount)
			   assignAccount.Account = fzStores.Z5
			   assignAccount.AssignRatio = fzitems.R5
			   assignAccount.AssignMoney = (value.Payment+value.ItemTax)*assignAccount.AssignRatio/100
			   assignAll.AssignAccounts = append(assignAll.AssignAccounts, assignAccount)
		   }
		   assignArr = append(assignArr, assignAll)
	   }
	}
	m := make(map[string]float32)
	for _, value := range assignArr {
	   for _, value1 := range value.AssignAccounts {
	      if v, ok := m[value1.Account]; ok {
		     m[value1.Account] = v + value1.AssignMoney
	      }else{
		     m[value1.Account] = value1.AssignMoney
		  }
	   }
	}
	accountAll := new(AccountAll)
	for key, value := range m {
	   accountItem := new(AccountItem)
	   accountItem.Account = key
	   accountItem.AssignMoney = value
	   accountAll.AccountItems = append(accountAll.AccountItems, accountItem)
	   accountAll.Total = accountAll.Total + value
	}
	return accountAll, nil
}

